package com.sunyard.client2;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Enumeration;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import com.sunyard.client2.conf.Editor;
import com.sunyard.client2.ui.ResultMessageBox;
import com.sunyard.client2.ui.UIFactory;

public class EcmClientTest extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
		try {
			UIManager.setLookAndFeel(lookAndFeel);
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EcmClientTest frame = new EcmClientTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EcmClientTest() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1000, 618);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnNewMenu = new JMenu("文件");
		menuBar.add(mnNewMenu);

		JMenuItem mntmNewMenuItem_2 = new JMenuItem("MENU");
		mnNewMenu.add(mntmNewMenuItem_2);

		JMenuItem mntmNewMenuItem_1 = new JMenuItem("退出");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mnNewMenu.add(mntmNewMenuItem_1);

		JMenu mnNewMenu_1 = new JMenu("帮助");
		menuBar.add(mnNewMenu_1);

		JMenuItem mntmNewMenuItem = new JMenuItem("配置");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Editor d = new Editor(EcmClientTest.this, true);
				d.setBounds(200, 200, 750, 550);
				d.setVisible(true);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem);

		JMenuItem mntmNewMenuItem_3 = new JMenuItem("关于");
		mnNewMenu_1.add(mntmNewMenuItem_3);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		final JPanel center = new JPanel();
		center.setLayout(new BorderLayout(0, 0));
		contentPane.add(center, BorderLayout.CENTER);
		DefaultMutableTreeNode test = new DefaultMutableTreeNode("测试 ", true);
		DefaultMutableTreeNode second = new DefaultMutableTreeNode("次要接口");
		DefaultMutableTreeNode first = new DefaultMutableTreeNode("重要接口");
		test.add(second);
		test.add(first);
		second.add(new DefaultMutableTreeNode(new NodeAction("Login", "登录")));
		second.add(new DefaultMutableTreeNode(new NodeAction("Check", "检出")));
		second.add(new DefaultMutableTreeNode(new NodeAction("Model", "模型模板")));
		second.add(new DefaultMutableTreeNode(new NodeAction("AllModel", "所有模型")));
		second.add(new DefaultMutableTreeNode(new NodeAction("DMServer", "服务器")));
		second.add(new DefaultMutableTreeNode(new NodeAction("GetNode", "所有服务器")));
		second.add(new DefaultMutableTreeNode(new NodeAction("Permission", "用户模型")));

		first.add(new DefaultMutableTreeNode(new NodeAction("Upload", "上传")));
		first.add(new DefaultMutableTreeNode(new NodeAction("Query", "查询")));
		first.add(new DefaultMutableTreeNode(new NodeAction("Update", "更新")));
		first.add(new DefaultMutableTreeNode(new NodeAction("HeightQuery", "高级查询")));

		final JTree tree = new JTree(test);
		expandTree(tree);
		final UIFactory uIFactory = new UIFactory();
		MouseListener ml = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				int selRow = tree.getRowForLocation(e.getX(), e.getY());
				if (selRow != -1) {
					DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
					Object obj = node.getUserObject();
					if (obj instanceof NodeAction) {
						NodeAction action = (NodeAction) obj;
						System.out.println(action.getCode());
						center.removeAll();
						JPanel p = uIFactory.getPanel(action.getCode());
						if (p != null) {
							center.add(p, BorderLayout.CENTER);
						}
						center.updateUI();
					}
				}
			}
		};
		tree.addMouseListener(ml);
		contentPane.add(tree, BorderLayout.WEST);
		contentPane.add(ResultMessageBox.getInstance(), BorderLayout.SOUTH);
	}

	public void expandTree(JTree tree) {
		TreeNode root = (TreeNode) tree.getModel().getRoot();
		expandAll(tree, new TreePath(root), true);
	}

	private void expandAll(JTree tree, TreePath parent, boolean expand) {
		// Traverse children
		TreeNode node = (TreeNode) parent.getLastPathComponent();
		if (node.getChildCount() >= 0) {
			for (Enumeration e = node.children(); e.hasMoreElements();) {
				TreeNode n = (TreeNode) e.nextElement();
				TreePath path = parent.pathByAddingChild(n);
				expandAll(tree, path, expand);
			}
		}

		// Expansion or collapse must be done bottom-up
		if (expand) {
			tree.expandPath(parent);
		} else {
			tree.collapsePath(parent);
		}
	}

}
